package com.stedu.service.impl;

import com.stedu.bean.RepairTask;
import com.stedu.exception.SteduException;
import com.stedu.mapper.RepairTaskMapper;
import com.stedu.service.RepairTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class RepairTaskServiceImpl implements RepairTaskService {
    @Autowired
    private RepairTaskMapper repairTaskMapper;
    @Override
    public List<RepairTask> selectAll() {
        return repairTaskMapper.selectAll();
    }

    @Override
    public List<Long> selectRidByTid(Integer tid) {
        return repairTaskMapper.selectRidByTid(tid);
    }

    @Override
    public void insertTidAndRid(Integer tid, Long[] rids) {
        repairTaskMapper.deleteTidAndByRid(tid);
        if(rids.length != 0) {
            repairTaskMapper.insertTidAndRid(tid, rids);
        }
    }

    @Override
    public boolean insert(RepairTask rt) {
        rt.setRstate(0);
        return repairTaskMapper.insert(rt);
    }

    @Override
    public boolean update(RepairTask rt) {
        return repairTaskMapper.update(rt);

    }

    @Override
    public RepairTask selectByTid(Integer tid) {

        return repairTaskMapper.selectByTid(tid);
    }

    @Override
    public boolean delete(Integer tid) {
        return repairTaskMapper.delete(tid)==1;
    }

    @Override
    public List<RepairTask> selectTaskByRid(Integer rid) throws SteduException {
        List<Integer> tids = repairTaskMapper.selectTidByRid(rid);
        if (tids.isEmpty()) {
            throw new SteduException("当前没有维修任务");
        }
        List<RepairTask> repairtaskList = repairTaskMapper.selectByTids(tids);
        return repairtaskList;
    }
    public boolean deleteRidAndByTid(Integer rid) {
        return repairTaskMapper.deleteRidAndByTid(rid);
    }

}
